package com.hankz.cc.leetcode;

import java.util.Arrays;

/**
 * @auther zzc
 * @date 2021/4/9 14:30
 */
public class 移动零 {

    //输入 [1,2,0,9,8,0,5]
    //输出 [1,2,9,8,5,0,0]
    //把0移到数组最后面

    public static void main(String[] args) {
        int[] arr = new int[]{1, 2, 5, 6, 0, 9, 6, 0, 4, 7, 0};
        System.out.println(move(arr).toString());
    }

    private static int[] move(int[] arr) {
        int i = 0;
        int j = 0;
        int count = 1;
        int[] res = new int[arr.length];
        while (i < arr.length && j < arr.length) {
            if (arr[i] != 0) {
                res[j] = arr[i];
                j++;
            } else {
                res[arr.length - count++] = 0;
            }
            i++;

        }
        int[] ints = Arrays.copyOfRange(res, 0, arr.length);
        return ints;
    }
}
